﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Logging;
using System.Data;
using VAdvantage.Utility;
using PdfSharp.Drawing;
using VAdvantage.Classes;

namespace VAdvantage.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class MPrintColor : X_AD_PrintColor
    {
        //	Static Logger	
        private static VLogger _log = VLogger.GetVLogger(typeof(MPrintColor).FullName);
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="rs">DataRow</param>
        /// <param name="trxName">Transaction</param>
        public MPrintColor(Ctx ctx, DataRow rs, String trxName)
            : base(ctx, rs, trxName)
        {
        }	//	MPrintColor

        static MPrintColor Create(XColor color, String name)
        {
            MPrintColor pc = new MPrintColor(VAdvantage.Utility.Envs.GetContext(), 0, null);
            pc.SetName(name);
            pc.SetColor(color);
            pc.Save();
            return pc;
        }	//	create



        /** Dark Green			*/
        public static XColor darkGreen = XColors.DarkGreen;
        /** Black Green			*/
        public static XColor blackGreen = XColors.Black;
        /** Dark Blue			*/
        public static XColor darkBlue = XColors.DarkBlue;
        /** Black Blue			*/
        public static XColor blackBlue = XColors.BlueViolet;
        /** White Gray			*/
        public static XColor whiteGray = XColors.Gray;
        /** Brown				*/
        public static XColor brown = XColors.Brown;
        /** Dark Brown			*/
        public static XColor darkBrown = XColors.SandyBrown;

        static private CCache<int, MPrintColor> _colors = new CCache<int, MPrintColor>("AD_PrintColor", 20);


        static public MPrintColor Get(Ctx ctx, int AD_PrintColor_ID)
        {
            //	if (AD_PrintColor_ID == 0)
            //		return new MPrintColor (ctx, 0);
            //Info
            int key = AD_PrintColor_ID;
            MPrintColor pc = (MPrintColor)_colors[key];
            if (pc == null)
            {
                pc = new MPrintColor(ctx, AD_PrintColor_ID, null);
                _colors.Add(key, pc);
            }
            return pc;
        }	//	get

        static public MPrintColor Get(Ctx ctx, String AD_PrintColor_ID)
        {
            if (AD_PrintColor_ID == null || AD_PrintColor_ID.Length == 0)
                return null;
            try
            {
                int id = int.Parse(AD_PrintColor_ID);
                return Get(ctx, id);
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, "AD_PrintColor_ID=" + AD_PrintColor_ID
                + " - " + e.ToString());
            }
            return null;
        }


        public MPrintColor(Ctx ctx, int AD_PrintColor_ID, String trxName)
            : base(ctx, AD_PrintColor_ID, trxName)
        {
            if (AD_PrintColor_ID == 0)
                SetIsDefault(false);
        }	//	MPrintColor

        private XColor? _cacheColor = null;

        public XColor GetColor()
        {
            if (_cacheColor != null)
            {
                return (XColor)_cacheColor;
            }
            String code = GetCode();
            if (code == null || code.Equals("."))
            {
                _cacheColor = XColors.Black;
            }
            try
            {
                if (code != null && !code.Equals("."))
                {
                    int rgba = int.Parse(code);
                    _cacheColor = XColors.Black;
                }
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, "MPrintColor.getColor", e);
            }
            if (code == null)
            {
                _cacheColor = XColors.Black;
            }
            //	//log.fine( "MPrintColor.getColor " + code, m_cacheColor);
            if (_cacheColor != null)
            {
                return (XColor)_cacheColor;
            }
            else
            {
                return XColor.Empty;
            }
        }

        /// <summary>
        /// Get Color
        /// </summary>
        /// <param name="color"></param>
        public void SetColor(XColor color)
        {
            int rgba = 4545;
            base.SetCode(rgba.ToString());
        }

    }
}
